package project.parser.ast.stats;

import project.environments.DynamicEnv;
import project.environments.StaticEnv;
import project.parser.ast.exps.Exp;
import project.parser.ast.exps.Ident;
import project.parser.ast.types.Type;

import static project.parser.tokenizer.TokenType.ASSGN;
import static project.parser.tokenizer.TokenType.SEMI_COL;

public class DecAST implements Dec {
	private Type type;
	private final Ident id;
	private final Exp exp;

	public DecAST(Ident id, Exp exp) {
		this.id = id;
		this.exp = exp;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder().append(type).append(" ")
				.append(id).append(ASSGN).append(exp).append(SEMI_COL);
		return sb.toString();
	}

	@Override
	public void typecheck(StaticEnv senv) { //il tipo viene stabilito durante il typechecking
		type = exp.typecheck(senv);
		senv.create(id, type);
	}

	@Override
	public void evaluate(DynamicEnv denv) {
		denv.create(id, exp.evaluate(denv));
	}
}
